using System.Windows.Controls.Primitives;
using DimensionsHelper.Client.ViewModels.CsvFormatter;

namespace DimensionsHelper.Client.Views.CsvFormatter;

public partial class MenuOptionsPage
{
    public MenuOptionsPage(IServiceProvider serviceProvider)
    {
        InitializeComponent();
        DataContext = serviceProvider.GetRequiredService<MenuOptionsViewModel>();
        _rangeButtons =
        [
            LinkSheetLabelCellToggleButton, BackCellButton, SheetLabelCellButton,
            TableLabelRangeButton, BaseRangeButton, MenuSectionButton
        ];
    }

    private readonly ToggleButton[] _rangeButtons;

    private void UncheckInternal(ToggleButton toggleButton)
    {
        foreach (ToggleButton button in _rangeButtons)
        {
            if (button == toggleButton)
            {
                continue;
            }
            
            button.IsChecked = false;
        }
    }

    private void ShowBaseButton_OnUnchecked(object sender, RoutedEventArgs e)
    {
        ShowMultiBaseButton.IsChecked = false;
        ShowMultiBaseInOneCellToggleButton.IsChecked = false;
    }

    private void ShowMultiBaseButton_OnChecked(object sender, RoutedEventArgs e)
    {
        ShowBaseButton.IsChecked = true;
    }

    private void ShowMultiBaseInOneCellToggleButton_OnChecked(object sender, RoutedEventArgs e)
    {
        ShowMultiBaseButton.IsChecked = true;
    }

    private void ResetCurrentRangeButton_OnUnchecked(object sender, RoutedEventArgs e)
    {
        MenuOptionsViewModel viewModel = (MenuOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel;
    }

    private void LinkSheetLabelCellToggleButton_OnChecked(object sender, RoutedEventArgs e)
    {
        MenuOptionsViewModel viewModel = (MenuOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.LinkSheetLabelStyle;
        UncheckInternal(LinkSheetLabelCellToggleButton);
    }

    private void BackCellButton_OnChecked(object sender, RoutedEventArgs e)
    {
        MenuOptionsViewModel viewModel = (MenuOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.BackTextStyle;
        UncheckInternal(BackCellButton);
    }

    private void SheetLabelCellButton_OnChecked(object sender, RoutedEventArgs e)
    {
        MenuOptionsViewModel viewModel = (MenuOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.SheetLabelStyle;
        UncheckInternal(SheetLabelCellButton);
    }

    private void TableLabelRangeButton_OnChecked(object sender, RoutedEventArgs e)
    {
        MenuOptionsViewModel viewModel = (MenuOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.TableLabelStyle;
        UncheckInternal(TableLabelRangeButton);
    }

    private void BaseRangeButton_OnChecked(object sender, RoutedEventArgs e)
    {
        MenuOptionsViewModel viewModel = (MenuOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.BaseStyle;
        UncheckInternal(BaseRangeButton);
    }

    private void MenuSectionButton_OnChecked(object sender, RoutedEventArgs e)
    {
        MenuOptionsViewModel viewModel = (MenuOptionsViewModel)DataContext;
        viewModel.CurrentRange = viewModel.SectionStyle;
        UncheckInternal(MenuSectionButton);
    }
}